//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    yesOrNo: "签到",
    opacity: 1,
    backImg0: "../../images/signBg.jpg",
    backImg1: "../../images/sign.png",
    signData: [],
    signAlready: "",
    days: [{
        src: "../../images/sign0.png",
        M: 0.5,
        active: false
      },
      {
        src: "../../images/sign0.png",
        M: 0.5,
        active: false
      },
      {
        src: "../../images/sign0.png",
        M: 0.5,
        active: false
      },
      {
        src: "../../images/sign0.png",
        M: 0.5,
        active: false
      },
      {
        src: "../../images/sign0.png",
        M: 0.5,
        active: false
      },
      {
        src: "../../images/sign0.png",
        M: 0.5,
        active: false
      },
      {
        src: "../../images/sign0.png",
        M: 0.5,
        active: false
      },
      {
        src: "../../images/sign0.png",
        M: 0.5,
        active: false
      },
      {
        src: "../../images/sign0.png",
        M: 0.5,
        active: false
      },
      {
        src: "../../images/sign0.png",
        M: 0.5,
        active: false
      },
      {
        src: "../../images/sign0.png",
        M: 0.5,
        active: false
      },
      {
        src: "../../images/sign0.png",
        M: 0.5,
        active: false
      },
      {
        src: "../../images/sign0.png",
        M: 0.5,
        active: false
      },
      {
        src: "../../images/sign0.png",
        M: 0.5,
        active: false
      },
      {
        src: "../../images/sign0.png",
        M: 0.5,
        active: false
      },
    ],
    // 
    uid: "",
    userType: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType')
    })
    this.GetUserIsSignAndSignData();
  },

  // rulesTap
  rulesTap: function() {
    wx.showModal({
      title: '提示',
      content: '签到终止就要重新开始,连续签到15天可获得更多易M!',
      showCancel: false
    })
  },
  //  GetUserIsSignAndSignData
  GetUserIsSignAndSignData: function() {
    var that = this;
    var uid = this.data.uid;
    var days = this.data.days;
    var signData = this.data.signData;
    var signAlready = this.data.signAlready;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetUserIsSignAndSignData&UID=" + uid),
      success: function(res) {
        signData = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        signAlready = signData.SignNum
        that.setData({
          signData: signData,
          signAlready: signAlready
        })
        if (signData.IsSign == '1') {
          that.setData({
            yesOrNo: "已签到",
            opacity: 0.8,
          })
        }
        for (var i = 0; i < days.length; i++) {
          if (i < signAlready) {
            days[i].active = true;
            days[i].src = "../../images/sign1.png";
            that.setData({
              days: days
            })
          } else {

          }
        }
      }
    })
  },
  // 签到
  signTap: function() {
    var that = this;
    var uid = this.data.uid;
    var days = this.data.days;
    var yesOrNo = this.data.yesOrNo;
    var signAlready = this.data.signAlready;
    if (yesOrNo == "已签到") {
      wx.showModal({
        title: '提示',
        content: '不可重复签到呦！',
        showCancel: false
      })
    } else {
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=EveryDaySign1&UID=" + uid),
        success: function(res) {
          var json = JSON.parse(baseJs.decryUrl(res.data));
          signAlready = json.ResultData;
          console.log(json);
          if (json.ResultFlag == 1) {
            for (var i = 0; i < days.length; i++) {
              if (i < signAlready) {
                days[i].active = true;
                days[i].src = "../../images/sign1.png";
                that.setData({
                  days: days,
                  signAlready: signAlready,
                })
              } else {

              }
            }
            wx.showToast({
              title: json.ResultMsg,
              icon: 'none',
              duration: 2000
            });
            that.setData({
              yesOrNo: "已签到",
              opacity: 0.8,
            })
          } else {
            wx.showToast({
              title: json.ResultMsg,
              icon: 'none',
              duration: 2000
            });
          }
        }
      })
    }
  },
  // 
})